/**
 * 
 */
package com.base.myproject.client.busi.service;

import java.util.Date;

import com.base.myproject.client.busi.ClientEnv.CcodeSync;
import com.base.myproject.client.busi.ClientEnv.SyncObjectContainer;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.Loading;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;

/**
 * 添加一个会员，
 * 
 * @author zgw 2009-11-22 上午06:38:57 javazgw@gmail.com
 */
public class AddOneCcodeDialog implements Listener<BaseEvent>,AsyncCallback<String>{
	TextField<String> s_cardno =new TextField<String>();;
	TextField<String> cname = new TextField<String>();;
	TextField<String> s_mobil = new TextField<String>();;
	BaseComboBoxForm<BaseModelData> sex_t = CodeNameMapFactory
			.createSexInput_F();
	TextField<String> edu_t = new TextField<String>();
	DateField bird_t = new DateField();
	TextField<String> idno_t = new TextField<String>();
	TextField<String> mark_t = new TextField<String>();
	TextField<String> password_t = new TextField<String>();
	TextField<String> passwordc_t = new TextField<String>();

	
	// activeFormat.format(new Date());
	// getStore().addDefault("createbcode", Env.getEnvbcode());
	// getStore().addDefault("createbname", Env.getEnvname());
	// getStore().addDefault("createdate", activeFormat.format(new Date()));
	FormPanel formpanel = new FormPanel();
	
	Dialog d = new Dialog();

	public AddOneCcodeDialog() {

	}

	public void showDialog() {

		d.setWidth("100");
		d.setHeading("添加会员");
		s_cardno.setFieldLabel("会员卡号");
		// s_cardno.addListener(Events.Change, this);
		cname.setFieldLabel("会员名称");
		s_mobil.setFieldLabel("会员电话");
		sex_t.setFieldLabel("性别");
		
		edu_t.setFieldLabel("学历");
		bird_t.setFieldLabel("生日");
		idno_t.setFieldLabel("身份证号");
		mark_t.setFieldLabel("备注");
		password_t.setFieldLabel("密码");
		passwordc_t.setFieldLabel("密码确认");
		
		
		s_cardno.setAllowBlank(false);
		cname.setAllowBlank(false);
		s_mobil.setAllowBlank(false);
		sex_t.setAllowBlank(false);
		password_t.setPassword(true);
		passwordc_t.setPassword(true);
		
		formpanel.add(s_cardno);
		formpanel.add(cname);
		formpanel.add(s_mobil);
		formpanel.add(sex_t);
		//formpanel.add(edu_t);
		formpanel.add(bird_t);
		//formpanel.add(idno_t);
		formpanel.add(mark_t);
		formpanel.add(password_t);
		formpanel.add(passwordc_t);
		formpanel.add(new HTML("<br><a>如果会员不需要设置密码，请保持内容为空，否则会员在消费的时候会要求录入密码。</a>"));

		d.setSize(260, 400);
		d.add(formpanel);
		d.setButtons(Dialog.OKCANCEL);
		d.getButtonById(Dialog.CANCEL).addListener(Events.Select, this);
		d.getButtonById(Dialog.OK).addListener(Events.Select, this);
		
		d.show();
	}

	@Override
	public void handleEvent(BaseEvent be) {
		  if(be.getSource() == d.getButtonById(Dialog.CANCEL))
			{
				d.setVisible(false);
			}
			else if(be.getSource() == d.getButtonById(Dialog.OK))
			{
				sendccode();
			}
		  
//			else if(be.getSource() == addfinace)
//			{
//				AddFinace_acc addfinace_acc = new AddFinace_acc();
//				addfinace_acc.showDialog();
//			}
				
				
		
	}
	

	private void sendccode()
	{
		
		if(formpanel.isValid() && ((password_t.getValue()==null&&passwordc_t.getValue()==null)||( password_t.getValue().equals(passwordc_t.getValue()))))
		{
			
			CcodeSync ccodesync = new CcodeSync();
			ccodesync.setCname(cname.getValue());
			ccodesync.setS_mobil(s_mobil.getValue());
			ccodesync.setSex(sex_t.getValue().get("code")+"");
			ccodesync.setS_cardno(s_cardno.getValue());
			ccodesync.setPassword(password_t.getValue());
			ccodesync.setS_mark(mark_t.getValue());
			ccodesync.setUpdatebcode(Env.getEnvbcode());
			ccodesync.setUpdatebname(Env.getEnvname());
			ccodesync.setUpdatedate(new Date());
			
			SyncObjectContainer soc = new SyncObjectContainer();
			soc.setSyncobject(ccodesync);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getSyncObject(soc,1,this);
		d.getButtonById(Dialog.OK).setEnabled(false);
//		 DOM.setInnerHTML(RootPanel.get("Loading-Message").getElement(), "<img src=\"images/baseimage/ajax-loader.gif\"  alt=\"loading\"> Loading GWT Application, please wait...");
		 Loading.getinstance().loading();
		}
		else {
			MessageBox.alert("提示", "请检查必录项、录入格式不对、密码是否一致。", null);
		}
	}

	@Override
	public void onFailure(Throwable caught) {
		Loading.getinstance().loaded();
		MessageBox.alert("警告", "错误!通信错误，请检查网络连接。", null);
		d.el().blink(FxConfig.NONE); 
//		设置ok按钮可以被按下，在发送会等待网络的时候ok按钮被禁止，此处取消禁止
		d.getButtonById(Dialog.OK).setEnabled(true);
	}

	@Override
	public void onSuccess(String result) {
		Loading.getinstance().loaded();
		if(result.equals( SystemMessage.getMessage(4, 9)))
		{
			Info.display("",result);
			new AddFinace().showDialog();
			
		}
		else
		{
			d.el().blink(FxConfig.NONE);  
			MessageBox.alert("警告", "错误:"+result, null);
//			设置ok按钮可以被按下，在发送会等待网络的时候ok按钮被禁止，此处取消禁止
			d.getButtonById(Dialog.OK).setEnabled(true);
		}
//		Info.display("",result);
//		d.el().blink(FxConfig.NONE);  
		
	}
}
